<template>
    <div class="authority">
        <div class="gva-table-box unit">
            <div class="gva-btn-list">
                <el-button type="primary" icon="plus" @click="addUnit">新增</el-button>
            </div>
            <el-table :data="tableData" row-key="id">
                <el-table-column type="index" label="序号" width="100" />
                <el-table-column align="left" label="地址坐落" prop="address"></el-table-column>
                <el-table-column align="left" label="用地面积" prop="useArea"></el-table-column>
                <el-table-column align="left" label="总建筑面积" prop="totalArea"></el-table-column>
                <el-table-column align="left" label="权属登记情况" show-overflow-tooltip>
                    <template #default="scope">
                        <span>{{ qsdjList[scope.row.ownership-1].name }}</span>
                    </template>
                </el-table-column>
                <el-table-column align="left" label="建设年代" prop="createYear"></el-table-column>
                <el-table-column align="left" label="是否独立办公">
                    <template #default="scope">
                        <span>{{ scope.row.independence == 1 ? '独立办公' : '合署办公' }}</span>
                    </template>
                </el-table-column>
                <el-table-column align="left" fixed="right" label="操作" min-width="160">
                    <template #default="scope">
                        <el-button type="primary" link icon="edit" @click="editUnit(scope.row)">
                            编辑
                        </el-button>
                        <el-button type="danger" link icon="delete" @click="delUnit(scope.row)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="gva-pagination">
                <el-pagination v-model:current-page="page" :page-size="pageSize" :total="total"
                    layout="prev, pager, next" @current-change="handleCurrentChange" />
            </div>
        </div>
        <el-drawer v-model="dialogFormVisible" :size="appStore.drawerSize" :before-close="handleClose"
            :show-close="false">
            <template #header>
                <div class="flex justify-between items-center">
                    <span class="text-lg">{{ dialogTitle }}</span>
                    <div>
                        <el-button @click="closeDialog">取消</el-button>
                        <el-button type="primary" @click="enterDialog">确定</el-button>
                    </div>
                </div>
            </template>
            <el-form v-if="dialogFormVisible" ref="yyglForm" label-width="150" :model="form" :rules="rules">
                <el-form-item label="是否独立办公" prop="independence">
                    <el-radio-group v-model="form.independence" class="ml-4">
                        <el-radio :value="1" size="large">独立办公</el-radio>
                        <el-radio :value="2" size="large">合署办公</el-radio>
                    </el-radio-group>
                </el-form-item>
                <div style="color: #ff6666;margin: -20px 0 16px 60px;">注：【独立办公】技术业务用房内无其他单位共用；【合署办公】多家单位共用此技术业务用房</div>
                <el-form-item label="地址坐落" prop="address">
                    <el-input v-model="form.address" placeholder="请输入" style="width: 400px;"/>
                </el-form-item>
                <el-form-item label="建设年代" prop="createYear">
                    <el-input v-model="form.createYear" placeholder="请输入" style="width: 400px;"/>
                </el-form-item>
                <el-form-item label="用地面积" prop="useArea">
                    <el-input v-model="form.useArea" placeholder="请输入" style="width: 400px;"/>
                    <span style="margin-left: 4px;">平方米</span>
                </el-form-item>
                <el-form-item label="总建筑面积" prop="totalArea">
                    <el-input v-model="form.totalArea" placeholder="请输入" style="width: 400px;"/>
                    <span style="margin-left: 4px;">平方米</span>
                </el-form-item>
                <el-form-item label="权属登记情况" prop="ownership">
                    <el-select v-model="form.ownership" placeholder="请选择" style="width: 400px;">
                        <el-option v-for="item in qsdjList" :key="item.id" :label="item.name" :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="form.notes" type="textarea" :rows="6" />
                </el-form-item>
            </el-form>
        </el-drawer>
    </div>
</template>
<script setup>
import {
    technologyList,
    deleteTechnology,
    addTechnology,
    editTechnology
} from '@/api/unit'
import {qsdjList} from '@/utils/constant'
import { reactive, ref, onMounted, computed } from 'vue'
import { useAppStore } from "@/pinia";
import { useUserStore } from '@/pinia/modules/user'
import { ElMessage, ElMessageBox, useFormItem } from 'element-plus'


// 通过该函数，可以获取组件当前所在的表单项formItem
const { formItem } = useFormItem()

defineOptions({
    name: 'Yygl'
})
const appStore = useAppStore()
const form = ref({
    independence: 1,
    address: '',
    createYear: '',
    notes: '',
    ownership: '',
    totalArea: '',
    unitId: '',
    useArea: ''
})

const searchInfo = ref({
    name: ''
})

const userStore = useUserStore()
const headerOdj = ref({ token: userStore.token })
const adminUrl = ref('')
adminUrl.value = import.meta.env.VITE_BASE_PATH + '/file/upload'
const page = ref(1)
const total = ref(0)
const pageSize = ref(10)
const dialogFormVisible = ref(false)
let validateImage = (rule, value, callback) => {
    if (!form.value.ico) {
        callback(new Error('请上传图片'))
    } else {
        callback()
    }
}
const rules = reactive({
    address: [{ required: true, message: '请输入地址坐落', trigger: 'blur' }],
    independence: [{ required: true, message: '请选择职级分类等级', trigger: 'change' }],
    useArea: [{ required: true, message: '请输入用地面积', trigger: 'blur' }],
    totalArea: [{ required: true, message: '请输入总建筑面积', trigger: 'blur' }],
})

const yyglForm = ref(null)

const initForm = () => {
    yyglForm.value.resetFields()
    form.value = {
        independence: 1,
        address: '',
        createYear: '',
        notes: '',
        ownership: '',
        totalArea: '',
        unitId: '',
        useArea: ''
    }
}

const isEdit = ref(false)
const dialogTitle = ref('新增')
const addUnit = () => {
    dialogTitle.value = '新增'
    isEdit.value = false
    dialogFormVisible.value = true
}
const closeDialog = () => {
    initForm()
    dialogFormVisible.value = false
}

const handleClose = (done) => {
    initForm()
    done()
}
const handleUserClose = (done) => {
    initUserForm()
    done()
}

const changeRadio = () => {
    form.value.realName = []
}
const handleCurrentChange = (val) => {
    page.value = val
    getTableData()
}
const tableData = ref([])
// 查询
const getTableData = async () => {
    if (localStorage.getItem('userInfo')) {
        searchInfo.value.unitId = JSON.parse(localStorage.getItem('userInfo')).unitId
        form.value.unitId = JSON.parse(localStorage.getItem('userInfo')).unitId
    }
    const res = await technologyList(searchInfo.value.unitId)
    if (res.code == 200) {
        tableData.value = res.result
        total.value = res.result.total
    }
}

onMounted(() => {
    getTableData()
})

const enterDialog = async () => {
    yyglForm.value.validate(async (valid) => {
        if (valid) {
            let res
            if (isEdit.value) {
                res = await editTechnology(form.value)
            } else {
                res = await addTechnology(form.value)
            }
            if (res.code == 200) {
                ElMessage({
                    type: 'success',
                    message: isEdit.value ? '编辑成功' : '添加成功!'
                })
                getTableData()
            }
            initForm()
            dialogFormVisible.value = false
        }
    })
}

const delUnit = (row) => {
    ElMessageBox.confirm(
        '确认删除?',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }
    ).then(async () => {
        const res = await deleteTechnology(row.id)
        if (res.code === 200) {
            ElMessage({
                type: 'success',
                message: '删除成功!'
            })
            getTableData()
        }
    }).catch(() => {
        ElMessage({
            type: 'info',
            message: '已取消删除'
        })
    })
}

const editUnit = async (row) => {
    isEdit.value = true
    dialogTitle.value = '编辑'
    form.value = JSON.parse(JSON.stringify(row))
    form.value.technologyRoomId = form.value.id
    dialogFormVisible.value = true
}
const statusChange = (id, val) => {
    changeStatus(id).then(res => {
        ElMessage({
            type: 'success',
            message: '状态修改成功!'
        })
        getTableData()
    })
}
const onSubmit = () => {
    page.value = 1
    getTableData()
}

const onReset = () => {
    searchInfo.value = {
        unitId: JSON.parse(localStorage.getItem('userInfo')).unitId
    }
    getTableData()
}


const fileList = ref([])
const handleRemove = (file) => {
    fileList.value = []
    form.value.ico = ''
}
const uploadSuccess = (res) => {
    if (res.code != 200) {
        ElMessage({
            type: 'error',
            message: '上传失败' + res.msg
        })
        fileList.value.pop()
        return
    }
    form.value.ico = res.result
    yyglForm.value.validateField('ico')
}

</script>

<style lang="scss" scoped>
.flex {
    .area {
        flex: 2;
        background-color: #fff;
        margin: 0.5rem 20px 0.5rem 0;
    }

    .unit {
        flex: 4;
    }
}

.ant-alert-info {
    display: flex;
    justify-content: end;
    align-items: center;
    background-color: #e6f7ff;
    border: 1px solid #91d5ff;
    color: #1890ff;
    padding: 5px;

    .anticon {
        display: flex;
        align-items: center;
        cursor: pointer;
    }
}

.pop-content {
    .pop-title {
        font-weight: bold;
        padding-bottom: 4px;
        border-bottom: 1px solid rgba(5, 5, 5, 0.06);
    }

    .el-checkbox-group {
        font-size: 0;
        line-height: 0;
        display: flex;
        flex-wrap: wrap;
    }
}
</style>